<template>
	<view class="container">
		<uni-group title="头衔申请开关">
			<view class="uni-list">
				<view class="uni-list-cell uni-list-cell-pd">
					<view class="uni-list-cell-db">公爵</view>
					<view v-if="formData.gjSw==1">
						<switch checked @change="switch1Change" />
					</view>
					<view v-else>
						<switch @change="switch1Change" />
					</view>
				</view>
				<view class="uni-list-cell uni-list-cell-pd">
					<view class="uni-list-cell-db">科学家</view>
					<view v-if="formData.kxjSw==1">
						<switch checked @change="switch2Change" />
					</view>
					<view v-else>
						<switch @change="switch2Change" />
					</view>
				</view>
				<view class="uni-list-cell uni-list-cell-pd">
					<view class="uni-list-cell-db">建筑师</view>
					<view v-if="formData.jzsSw==1">
						<switch checked @change="switch3Change" />
					</view>
					<view v-else>
						<switch @change="switch3Change" />
					</view>
				</view>
			</view>
		</uni-group>

		<uni-group title="头衔持续时间(单位:秒)">
			<uni-forms err-show-type="undertext" :value="formData" ref="form1" border="true" label-width="100">
				<uni-forms-item required label="公爵" name="dukeKeepTime">
					<uni-easyinput type="number" trim="all" v-model="formData.gj" placeholder="请输入持续时间,单位:秒" />
				</uni-forms-item>
				<uni-forms-item required label="科学家" name="scientistKeepTime">
					<uni-easyinput type="number" trim="all" v-model="formData.kxj" placeholder="请输入持续时间,单位:秒" />
				</uni-forms-item>
				<uni-forms-item required label="建筑师" name="architectKeepTime">
					<uni-easyinput type="number" trim="all" v-model="formData.jzs" placeholder="请输入持续时间,单位:秒" />
				</uni-forms-item>
				<button @click="submitForm1('form1')">修改</button>
			</uni-forms>
		</uni-group>

		<uni-group title="头衔口令">
			<uni-forms err-show-type="undertext" :value="formData" ref="form2" border="true" label-width="100">
				<uni-forms-item required label="口令" name="password">
					<uni-easyinput type="text" trim="all" v-model="formData.password" placeholder="请输入口令" />
				</uni-forms-item>
				<button @click="submitForm2('form2')">修改</button>
			</uni-forms>
		</uni-group>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message" :value="popupMessage">
				<uni-popup-message :type="popupMessage.msgType" :message="popupMessage.messageText" :duration="2000">
				</uni-popup-message>
			</uni-popup>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				formData: {
					serverId: getApp().globalData.user.serverId,
					unionName: '',
					titleName: '',
					coordinateX: getApp().globalData.user.coordinateX,
					coordinateY: getApp().globalData.user.coordinateY,
					invitationCode: '',
					serverCode: getApp().globalData.user.serverCode,
					password: '',
					title: 0,
					gj: '',
					kxj: '',
					jzs: '',
					gjSw: 1,
					kxjSw: 1,
					jzsSw: 1
				},
				popupMessage: {
					type: 'center',
					msgType: 'success',
					messageText: '这是一条成功提示',
					value: ''
				},
				rules: {
					gj: {
						rules: [{
							required: true,
							errorMessage: '请输入持续时间',
						}, {
							format: 'int',
							errorMessage: '必须是数字'
						}]
					},
					kxj: {
						rules: [{
							required: true,
							errorMessage: '请输入持续时间',
						}, {
							format: 'int',
							errorMessage: '必须是数字'
						}]
					},
					jzs: {
						rules: [{
							required: true,
							errorMessage: '请输入持续时间',
						}, {
							format: 'int',
							errorMessage: '必须是数字'
						}]
					},
					password: {
						rules: [{
							required: true,
							errorMessage: '请输入口令',
						}, {
							minLength: 2,
							maxLength: 20,
							errorMessage: '口令长度在 {minLength} 到 {maxLength} 个字符',
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.form1.setRules(this.rules)
			this.$refs.form2.setRules(this.rules)
		},
		methods: {
			switch1Change: function(e) {
				let that=this
				uni.request({
					url: getApp().globalData.url + '/api/title/updateTitleByServerId',
					method: "POST",
					header: {
						'content-type': 'application/json',
						'token':getApp().globalData.token
					},
					data: {serverId:getApp().globalData.user.serverId,dukeSwitch:e.detail.value?1:0},
					success: (res) => {
						if (res.data.code == "00000") {
							that.popupMessage.msgType = "success"
							that.popupMessage.messageText = "成功"
							that.$refs.message.open()
						} else {
							that.popupMessage.msgType = "error"
							that.popupMessage.messageText = res.data.msg
							that.$refs.message.open()
						}
					}
				});
				
			},
			switch2Change: function(e) {
				let that=this
				uni.request({
					url: getApp().globalData.url + '/api/title/updateTitleByServerId',
					method: "POST",
					header: {
						'content-type': 'application/json',
						'token':getApp().globalData.token
					},
					data: {serverId:getApp().globalData.user.serverId,scientistSwitch:e.detail.value?1:0},
					success: (res) => {
						if (res.data.code == "00000") {
							that.popupMessage.msgType = "success"
							that.popupMessage.messageText = "成功"
							that.$refs.message.open()
						} else {
							that.popupMessage.msgType = "error"
							that.popupMessage.messageText = res.data.msg
							that.$refs.message.open()
						}
					}
				});
			},
			switch3Change: function(e) {
				let that=this
				uni.request({
					url: getApp().globalData.url + '/api/title/updateTitleByServerId',
					method: "POST",
					header: {
						'content-type': 'application/json',
						'token':getApp().globalData.token
					},
					data: {serverId:getApp().globalData.user.serverId,architectSwitch:e.detail.value?1:0},
					success: (res) => {
						if (res.data.code == "00000") {
							that.popupMessage.msgType = "success"
							that.popupMessage.messageText = "成功"
							that.$refs.message.open()
						} else {
							that.popupMessage.msgType = "error"
							that.popupMessage.messageText = res.data.msg
							that.$refs.message.open()
						}
					}
				});
			},
			submitForm1: function(e) {
				let that = this
				uni.showLoading()
				this.$refs.form1.submit().then(update => {
					update.serverId=getApp().globalData.user.serverId
					update.dukeKeepTime=update.dukeKeepTime*1000
					update.scientistKeepTime=update.scientistKeepTime*1000
					update.architectKeepTime=update.architectKeepTime*1000
					uni.request({
						url: getApp().globalData.url + '/api/title/updateTitleByServerId',
						method: "POST",
						header: {
							'content-type': 'application/json',
							'token':getApp().globalData.token
						},
						data: update,
						success: (res) => {
							uni.hideLoading()
							if (res.data.code == "00000") {
								that.popupMessage.msgType = "success"
								that.popupMessage.messageText = "成功"
								that.$refs.message.open()
							} else {
								that.popupMessage.msgType = "error"
								that.popupMessage.messageText = res.data.msg
								that.$refs.message.open()
							}
						}
					});
					console.log('表单数据信息：', update);
				}).catch(err => {
					uni.hideLoading()
					console.log('表单错误信息：', err);
				})
			},
			submitForm2: function(e) {
				let that = this
				uni.showLoading()

				this.$refs.form2.submit().then(update => {
					update.serverId=getApp().globalData.user.serverId
					uni.request({
						url: getApp().globalData.url + '/api/title/updateTitleByServerId',
						method: "POST",
						header: {
							'content-type': 'application/json',
							'token':getApp().globalData.token
						},
						data: update,
						success: (res) => {
							uni.hideLoading()
							if (res.data.code == "00000") {
								that.popupMessage.msgType = "success"
								that.popupMessage.messageText = "成功"
								that.$refs.message.open()
							} else {
								that.popupMessage.msgType = "error"
								that.popupMessage.messageText = res.data.msg
								that.$refs.message.open()
							}
						}
					});
					console.log('表单数据信息：', update);
				}).catch(err => {
					uni.hideLoading()
					console.log('表单错误信息：', err);
				})
			},
			refresh: function(e) {
				uni.redirectTo({
					url: "/pages/applyTitle/applyTitle"
				})
			},
			formReset: function(e) {
				console.log('清空数据')
			},
			goHelp() {
				uni.navigateTo({
					url: "/pages/help/help"
				})
			},
			onLoad() {
				uni.request({
					url: getApp().globalData.url + '/api/title/selectTitleByServerId?serverId=' + getApp()
						.globalData.user
						.serverId,
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					success: (res) => {
						if (res.data.code == "00000") {
							var t = res.data.data
							this.formData.gj = t.dukeKeepTime / 1000
							this.formData.kxj = t.scientistKeepTime / 1000
							this.formData.jzs = t.architectKeepTime / 1000
							this.formData.password = t.password
							this.formData.gjSw = t.dukeSwitch
							this.formData.kxjSw = t.scientistSwitch
							this.formData.jzsSw = t.architectSwitch
						}
					}
				});


			}
		},
		onShareAppMessage: function(options) {
			var that = this;
			var shareObj = {
				path: '/pages/menu/menu'
			};
			return shareObj;
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}

	.container {
		padding: 20rpx;
	}
</style>
